package orest.front.collection;

import java.io.IOException;
import java.rmi.ServerException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import orest.front.collection.CollectionHandler;

import net.ssta.core.base.ServletBase;
import net.ssta.core.db.DBConnectionProvider;
import net.ssta.core.db.PagingInfo;
import net.ssta.core.db.Record;
import net.ssta.core.db.RecordSet;
import net.ssta.core.web.RequestKit;
import net.ssta.core.util.DateUtility;

import net.ssta.common.Constants;
import net.ssta.common.LoginUser;


/**
 @author zhanggk
 
 用户注册、用户中心等相关信息维护
 **/
public class CollectionServlet  extends ServletBase{

    protected void defaultAction(HttpServletRequest request,HttpServletResponse response) throws IOException
    {
    }
	
	/**
     * 添加用户留言
     * 
     * @param request
     * @param response
     */
    public void addCollection(HttpServletRequest request,HttpServletResponse response)
    {
    	Connection conn = null;
    	RequestKit requestKit=new RequestKit(request);
    	String forwardurl = "";
    	//System.out.println("addCollection+++++++++++++++++++++");
    	try {
    		//获取数据库链接
    		conn = DBConnectionProvider.getConnection(Constants.JNDI_SSTA);
    	
	    	//从页面上获取技术需求相关内容
	    	Record record = new Record();
	    	//栏目类别，对于CMS发布的信息，存储栏目uuid
	    	//技术市场，21：技术需求、22：自荐项目、23：投融资项目
	    	//成果信息，11：技术成果、12：科研机构、13：技术专家
	    	String channelid = requestKit.getParameter("channelid", "");
	    	//栏目名称
	    	String channelname = requestKit.getParameter("channelname", "");
	    	//标题
	    	String channeltitle = requestKit.getParameter("infotitle", "");
	    	//登录名
	    	String userid = LoginUser.getUserId();
	    	//收藏时间
	    	//信息id，信息对应的uuid
	    	String infoid = requestKit.getParameter("infoid", "");
	    	//收藏信息的访问URL
	    	String collectionurl = requestKit.getParameter("collectionurl", "");
	    	//System.out.println("++++++collectionurl "+collectionurl);
	    	
	    	//添加收藏信息表
	    	record.setEntityName("T_MYCOLLECTION");
	    	record.setConnection(conn);
	    	record.setPrimaryKey("ID");
	    	
	    	record.addData("COLUMNID", channelid);
	    	record.addData("COLUMNNAME", channelname);
	    	record.addData("TITLE", channeltitle);
	    	record.addData("COLLECTIONDATE", DateUtility.getCurrentDateTime());//提交日期为当前日期
	    	record.addData("USERID", userid); 		//获取登录人员uuid
	    	record.addData("COLUMNINFOID", infoid);
	    	record.addData("COLLECTIONURL", collectionurl);
	    	
	    	if(channelname!=null&&!"".equals(channelname)){
	    		record.create();
	    	}
	    	
		} catch(SQLException s){
			s.printStackTrace();
		}finally{
			//释放数据库连接
			if (conn != null)
	        {
				try
	            {
	                conn.close();
	            }catch (SQLException e){
	                e.printStackTrace();
	            }
	        }
		}	
    }
    
    
    /**
     * 获取用户收藏信息列表
     * 
     * @param request
     * @param response
     */
    public void collectionList(HttpServletRequest request,HttpServletResponse response)
    {
    	
			try {
				
				RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
		        PagingInfo pagingInfo = new PagingInfo(request, 10, "^(?!.*(result)|(LTPAToken)).*$");
		        
		        CollectionHandler collectionhandler = new CollectionHandler();
		        //根据登陆用户的uuid，获取该用户的收藏列表
		        RecordSet collectionSet = collectionhandler.getCollectionList(requestKit, pagingInfo);
		        
		        request.setAttribute("collectionSet", collectionSet);
		        request.setAttribute("pagingInfo", pagingInfo);
		        this.forwardByStyle("/membercenter/collection/collectionList.jsp");
		    	//forward("/membercenter/collection/collectionList.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException ex){
				ex.printStackTrace();
			}
		
    }
    
    /**
     * 根据收藏数据的类别，获取用户收藏信息列表
     * 
     * @param request
     * @param response
     */
    public void collectionClassList(HttpServletRequest request,HttpServletResponse response)
    {
    	
			try {
				
				RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
		        PagingInfo pagingInfo = new PagingInfo(request, 10, "^(?!.*(result)|(LTPAToken)).*$");
		        
		        CollectionHandler collectionhandler = new CollectionHandler();
		        //根据登陆用户的uuid，获取该用户的收藏列表
		        RecordSet collectionSet = collectionhandler.getCollectionClassList(requestKit, pagingInfo);
		        
		        request.setAttribute("collectionSet", collectionSet);
		        request.setAttribute("pagingInfo", pagingInfo);
		        this.forwardByStyle("/membercenter/collection/collectionList.jsp");
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ServletException ex){
				ex.printStackTrace();
			}
		
    }
    /**
     * 删除用户收藏信息
     * 
     * @param request
     * @param response
     */
    public void collectionDelete(HttpServletRequest request,HttpServletResponse response)
    {
    	RequestKit requestKit = (RequestKit) request.getAttribute("RequestKit");
        CollectionHandler collectionhandler = new CollectionHandler();
        
        String uuids = requestKit.getParameter("uuids", "");
        String uuid[] = uuids.split(",");
        if(uuid!=null && uuid.length>0){
        	for(int i=0;i<uuid.length;i++){
        		//逐一删除用户收藏信息
        		collectionhandler.collectionDelete(uuid[i]);
        	}
        }
    }
}


